<?php
    // get parameter
    //echo "fuck fuck";
    $pid = Yii::app()->request->getParam('pid');
    //Yii::log("fuck fuck");
    //echo Html::spaceTime("2011-07-19 11:41:30");
?>

<script type="text/javascript">
        function showNotebookCategory(id){
            $(".notebookWrapper").hide();
            $("#notebookWrapper-"+id).show();
            return false;
        }
	$(document).ready(function() {
                
                // process move page action
                $(".movePage").hide();
                
                $(".notebookMovePage").each(function(){
                   $(this).click(function(){
                       id  = $(this).attr('id');
                       nid = id.split('-')[1];
                       id = $(this).parents(".notebookWrapper").attr('id');
                       ctid = id.split('-')[1];
                       
                       $("#notebookControl-"+nid).append($(".movePage"));
                       $(".movePage").show();
                       $("select").val(ctid);
                       return false;
                   });
                });
                
                $(".movePageCancel").click(function(){
                   $(".notebookMovePage").show();
                   $(".movePage").hide();
                });
                
                $("select").change(function(){
                    // get id of element selected
                    id = $(this).parents(".notebookControl").attr('id');
                    nid = id.split('-')[1];
                    id = $(this).parents(".notebookWrapper").attr('id');
                    ctid = id.split('-')[1];
                    
                    //get value
                    new_ctid = $(this).val();
                    if (new_ctid == ctid)
                        return;
                    $.ajax({
                        url: '<?php echo $this->createUrl('/notebooks/movenote')?>',
                        type: 'POST',
                        data: {
                            nid:nid,
                            ctid:new_ctid
                        },
                        success: function(data){
                            
                            $("#notebookWrapper-"+new_ctid).append($("#notebookBlock-"+nid));
                            $(".movePageCancel").click();
                        },
                        error: function(){
                            alert("An error occured, please try again later");
                        }
                        
                    });
                });
                
                
		$(".popupCancel").click(function() {
			$.fancybox.close();
                });
                
                $(".notebookDelete").each(function (){
                        $(this).click(function(){
                            var answer = confirm ("Are you sure ?");
                            if (answer == false)
                                    return;
                            
                            id = $(this).attr('id');
                            nid = (id.split('-'))[1];
                            
                            $("#notebookBlock-"+nid).fadeOut();
                            $.ajax({
                                url: '<?php echo $this->createUrl('/notebooks/delnote')?>',
                                type: 'POST',
                                data: {
                                    nid:nid
                                },
                                success: function(data){
                                },
                                error: function(){
                                    alert("An error occured, please try again later");
                                }
                            });
                            return false;
                        });
                });
                
                $(".notebookCategoryDelete").each(function (){
                        $(this).click(function(){
                            var answer = confirm ("Are you sure ?");
                            if (answer == false)
                                    return;
                                
                            id = $(this).attr('id');
                            ctid = (id.split('-'))[1];
                            
                            $.ajax({
                                url: '<?php echo $this->createUrl('/notebooks/delcate')?>',
                                type: 'POST',
                                data: {
                                    pid:<?php echo Yii::app()->request->getParam('pid')?>,
                                    ctid:ctid
                                },
                                success: function(data){
                                    $("#notebookWrapper-"+ctid).fadeOut();
                                    $("#notebookCategory-"+ctid).fadeOut();

                                    $("option[value='"+ctid+"']").hide();
                                },
                                error: function(){
                                    alert("An error occured, please try again later");
                                }
                            });
                            return false;
                        });
                });
                
                // click function for edit detail of a notebook
                $(".notebookEditDetail").each(function (){
                        $(this).click(function(){
                            id = $(this).attr('id');
                            nid = id.split('-')[1];
                            
                            notebookName = $("#notebookName-"+nid).text();
                            $("#nbEditDetailName").val(notebookName);
                            $("#nid").val(nid);
                        });
                });
                
                // process form, use ajax.
                $("form").submit(function(){
                    
                    if ($(this).attr('action') == "updateCate" ){
                        ctid  = $("#ctid").val();
                        $.ajax({
                            url: '<?php echo $this->createUrl('notebooks/updateCate'); ?>',
                            type: 'POST',
                            data: {
                                ctid:ctid,
                                ctname:$(".editNBCName").val(),
                                ctdes:$("#editcateDes").val()
                            },
                            success: function(data){
                                $("#notebookWrapper-"+ctid).find("font").text($(".editNBCName").val());
                                $("#notebookWrapper-"+ctid).find(".cateDesciption").text($("#editcateDes").val());
                                $("#notebookCategory-"+ctid).find("a").text($(".editNBCName").val());

                                // rename in move category
                                $("option[value='"+ctid+"']").text($(".editNBCName").val());
                                $.fancybox.close();
                            },
                            error: function(){
                                alert("An error occured, please try again later");
                            }
                        });
                        return false;
                    }
                    
                    nid = $("#nid").val();
                    if ($(this).attr('action') == 'updateDetail')
                        $.ajax({
                            url: '<?php echo $this->createUrl('notebooks/updateDetail'); ?>',
                            type: 'POST',
                            data: {
                                detailName:$("#nbEditDetailName").val(),
                                nid:nid
                            },
                            success: function(data){
                                $("#notebookName-"+nid).text($("#nbEditDetailName").val());
                                $.fancybox.close();
                            }
                        });
                    return false;
                });
                
                // process edit button
                $(".editNBCLink").click(function(){
                    id = $(this).parents(".notebookWrapper").attr('id');
                    ctid = id.split('-')[1];
                    
                    ctname = $("#notebookWrapper-"+ctid).find("font").text();
                    ctdes  = $("#notebookWrapper-"+ctid).find(".cateDesciption").text();
                    
                    $(".editNBCName").val(ctname);
                    $("#editcateDes").val(ctdes);
                    $("#ctid").val(ctid);
                    return false;
                });
	});
	
</script>
<div class="clear"></div>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/notebook.css" />
<div id="than">	
    <img src="<?php echo Html::imageUrl("muiten.png")?>" width="27" height="26"; style="float:left">
            	<div id="textxanhdam">Notebooks </div>
 </div>
             <div class="padding15"></div>                    	
            	<!-- Bắt Đầu Cột Trái-->                  
              	<div id="leftSidebar">
              		<div id="leftWrapper">
						<div class="padding20"></div>
						<h3 class="nbAllCategory" onclick="javascript:$('.notebookWrapper').show();">All Category</h3>
                                                <!-- list category -->
						<ul>
                                                        <?php 
                                                            if ($notebookCategories != null)
                                                            foreach ($notebookCategories as $modelNoteBookCategory) {
                                                                $editTask = $modelNoteBookCategory['model'];
                                                        ?>
                                                            <li id="notebookCategory-<?php echo $editTask->notebook_category_id;?>"><a href="" onclick="javascript:showNotebookCategory('<?php echo $editTask->notebook_category_id;?>'); return false;"><?php echo $editTask->notebook_category_name;?></a></li>
                                                        <?php
                                                            }
                                                        ?>
						</ul>
                                                <!-- End list category -->
				</div>
				</div>
             	<!-- Kết Thúc Cột Trái-->
                <!-- Bắt Đầu Cột Phải-->
                <div id="rightSidebar">
                	<div id="rightHeader">
						<h2>Notebooks</h2>
						<button type="button" id="rightHeaderBtn" onclick="javascript:window.location ='<?php echo Yii::app()->controller->createUrl("/notebooks/addNotebook/",array("pid"=>$pid));?>'">Add a notebook</button>
					</div>
					<div id="rightWrapper">
                                                <!-- List All Notecategories with content-->
						<?php
                                                    if ($notebookCategories != null)
                                                    foreach ($notebookCategories as $modelNoteBookCategory) {
                                                        $editTask = $modelNoteBookCategory['model'];
                                                ?>
                                                <div class="notebookWrapper" id="notebookWrapper-<?php echo $editTask->notebook_category_id;?>">
							<div class="notebookWrapHeader"><font style="font-weight: bold;"><?php echo $editTask->notebook_category_name;?></font>
                                                            - <div class="cateDesciption" style="display:inline-block"><?php echo $editTask->notebook_category_description; ?></div>
                                                            <div class="notebookCatControl">
									<a href="#editNBCategory" class="editNBCLink">Edit</a>&nbsp |
									<a href="#" class="notebookCategoryDelete" id="notebookCategoryDelete-<?php echo $editTask->notebook_category_id;?>">Delete</a>
								</div>
							</div>
							<!-- List notebook block with a notebook category -->
                                                        <?php
                                                            foreach ($modelNoteBookCategory['listNotebooks'] as $notebook){
                                                                if ($notebook->notebook_updated_date == null)
                                                                    $notebook->notebook_updated_date = $notebook->notebook_created_date;
                                                        ?>
                                                        <div class="notebookBlock" id="notebookBlock-<?php echo $notebook->notebook_id ;?>">
								<a class="notebookName" href="<?php echo Yii::app()->controller->createUrl("/notebooks/detail/",array("pid"=>$pid,"nid"=>$notebook->notebook_id));?>" id="notebookName-<?php echo $notebook->notebook_id ;?>"><?php echo $notebook->notebook_name ;?></a>
								<img src="<?php echo Html::imageUrl("notebook.png");?>" class="notebookImage"></img>
								<div class="notebookDatetime">
									Created by 
                                                                        <a href="#" class="notebookAuthor"><?php echo $notebook->notebookPostedByUser->user_name; ?></a>
									Last Updated 
									<?php echo Html::spaceTime($notebook->notebook_updated_date); ?>.
								</div>
								<div class="notebookControl" id="notebookControl-<?php echo $notebook->notebook_id; ?>">
									<a href="#nbEditDetail" class="notebookEditDetail" id="notebookEditDetail-<?php echo $notebook->notebook_id; ?>">Edit details</a>&nbsp | &nbsp
									<a href="#" class="notebookDelete" id="notebookDelete-<?php echo $notebook->notebook_id; ?>">Delete</a>&nbsp | &nbsp
									<a href="#" class="notebookMovePage" id="notebookMovePage-<?php echo $notebook->notebook_id; ?>">Move page</a>
								</div>
							</div>
							<?php
                                                            }
                                                        ?>
                                                        <!-- end notebook block -->
							<div class="padding10"></div>
						</div>
						<?php
                                                    }
                                                ?>
                                                <!-- End Notebook Category -->
					</div>	
                </div>
                <!-- End right sidebar -->
                
    </div>
    
    <!-- Begin edit notebook category popup -->
	<div id="editNBCategoryPopup" style="display:none;">
		<div id="editNBCategory" class="popupWrapper" method="post" action="#">
			<div class="popupHeader">
				<h2>Edit notebook category</h2>
			</div>
			<div class="popupContent">
				<form method="post" action="updateCate">
					<h3>Category name</h3>
					<div class="padding5"></div>
					<input type="text" class="editNBCName" size="80px"></input>
					<div class="padding10"></div>
					<h3>Category description</h3>
					<textarea id="editcateDes"rows="4" cols="70"></textarea>
                                        <input type="hidden" name="ctid" id="ctid" />
					<div class="padding10"></div>
					<div class="postButton">
						<input type="submit" value="Update category" /> or
						<a href="#" class="popupCancel">Cancel</a>
					</div>
				</form>
			</div>
		</div>
	</div>
    
    
    <!-- Edit detail popup -->
    <div id="nbEditDetailPopup" style="display: none;">
	    <div id="nbEditDetail" class="popupWrapper" method="post" action="#">
	    	<div class="popupHeader">
	    		<h2>Edit notebook details</h2>
	    	</div>
	    	<div class="popupContent">
	    		<h3>Notebook name</h3>
	    		<div class="padding5"></div>
	    		<form method="post" action="updateDetail">
	    			<input type="text" id="nbEditDetailName"></input>
                                <input type="hidden" id="nid" />
	    			<div class="padding10"></div>
	    			<div class="postButton">
		    			<input type="submit" value="Update details" /> or
		    			<a href="#" class="popupCancel" id="cancel">Cancel</a>
		    		</div>
	    		</form>
	    	</div>
	    </div>
	</div>
    
        <div class="movePage">
                <select>
                        <!-- List all notebook category -->
                        <?php
                            foreach ($notebookCategories as $modelNoteBookCategory) {
                                $editTask = $modelNoteBookCategory['model'];
                                echo "<option value='{$editTask->notebook_category_id}'>{$editTask->notebook_category_name}</option>";
                            }
                        ?>
                </select> or 
                <a href="#" class="movePageCancel">Cancel</a>
        </div>
	
	<?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'a.notebookEditDetail', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false, 'hideOnOverlayClick' =>false ), ) ); ?>
	<?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'a.editNBCLink', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false, 'hideOnOverlayClick' =>false ), ) ); ?>